package com.example.administrator.yunniuusedcar.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.common.base.BaseActivity;
import com.example.administrator.yunniuusedcar.R;
import com.example.administrator.yunniuusedcar.adapter.SelectCarAdapter;
import com.example.administrator.yunniuusedcar.bean.SelectCarData;
import com.example.administrator.yunniuusedcar.model.mode_benn.IHttpService;
import com.example.administrator.yunniuusedcar.refresh.PullRefreshLayout;
import com.example.administrator.yunniuusedcar.util.Judge;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * 搜索
 * Created by Administrator on 2017/12/27.
 */

public class Search_SSActivity extends BaseActivity {
    @BindView(R.id.iv_Search2)
    ImageView ivSearch2;
    @BindView(R.id.tv_ScanCar)
    EditText tvScanCar;
    @BindView(R.id.Rv_Search)
    LinearLayout RvSearch;
    @BindView(R.id.tv_cancle)
    TextView tvCancle;

    private PullRefreshLayout mPull;
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private int pager = 1;

    private  SelectCarAdapter adapter;
    @Override
    public int getLayoutRes() {
        return R.layout.activity_search;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);


        swipeRefreshLayout = findView(R.id.swipelayout);
        mPull =  findView(R.id.pull);

        recyclerView = findView(R.id.recycler_view6);
        LinearLayoutManager linearLayoutManager =new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        adapter = new SelectCarAdapter(getApplicationContext());
        recyclerView.setAdapter(adapter);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.Rv_Search, R.id.tv_cancle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Rv_Search:  //搜索框
                Log.i("","");

                initUloadMenuInfor();

                break;
            case R.id.tv_cancle:
                finish();
                break;
        }
    }

    private void initUloadMenuInfor() {
        String ul = IHttpService.URL2 +IHttpService.GETCARBYNAME ;    // 搜索

        OkHttpUtils.post().url(ul)
                .addParams("sign",IHttpService.SignVal)
                .addParams("page",pager+"")
                .addParams("num","10")
                .addParams("name",tvScanCar.getText().toString())
                .build().execute(new StringCallback() {     //.addParams("type","1")   综合排序

            @Override
            public void onError(Call call, Exception e, int id) {
            }

            @Override
            public void onResponse(String response, int id) {
                //解析json
                handlerData(response);
            }
        });
    }

    private List<SelectCarData> dataList = new ArrayList<>();
    private void handlerData(String response) {
        try {
            swipeRefreshLayout.setRefreshing(false);
            JSONObject jsonObject = new JSONObject(response);
            String data = jsonObject.getString("data");
            JSONObject dataObject = new JSONObject(data);
            String datas = dataObject.getString("data");

            dataList .addAll(getDataList(datas));
            adapter.refreshData(dataList);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private List<SelectCarData> getDataList(String data_str) {
        if (!Judge.isJson(data_str)) {
            return null;
        }

        List<SelectCarData> datas = new ArrayList<>();
        try {
            JSONArray array = new JSONArray(data_str);

            for (int i = 0; i < array.length(); i++) {
                SelectCarData bean = new Gson().fromJson(array.get(i).toString(), SelectCarData.class);
                datas.add(bean);
            }
            return datas;
        } catch (JSONException e) {
            return null;
        }
    }
}
